import { _decorator, Component, Node, EditBox, Button } from 'cc';
import { BaseView } from '../../../../scripts/libs/rxjs/cc3/BaseView';
import { udm } from '../../../../scripts/managers/UserDataManager';
import { DevConfig } from '../../../../scripts/managers/DevConfig';
import { BindEditBoxInputComplete } from '../../../../scripts/libs/rxjs/cc3/BindEditBox';
import { BindButton } from '../../../../scripts/libs/rxjs/cc3/BindButton';
import { gm } from '../../../../scripts/managers/GameManager';
import { develop_func } from '../../develop_func';
import { profiler } from 'cc';
import { applovin_sdk_m } from '../../../../scripts/managers/SDK/AppLovinCocosSDKManager';
import { cloudstorage_sdk_m } from '../../../../scripts/managers/SDK/CloudStorageSDKManager';
import { notification_sdk_m } from '../../../../scripts/managers/SDK/NotificationCocosSDKManager';
import { timem } from '../../../../scripts/managers/TimeManager';
import { thinking_sdk_m } from '../../../../scripts/managers/SDK/ThinkingCocosSDKManager';
import { lm } from '../../LobbyManager';
import { FlowStatus } from '../../Enums';
import { am } from '../../../../scripts/managers/AudioManager';
const { ccclass, property } = _decorator;

@ccclass('DeveloperTools')
export class DeveloperTools extends BaseView {

    @property(EditBox)
    StageNumEditBox: EditBox = null;

    @property(EditBox)
    CoinCountEditBox: EditBox = null;

    @property(EditBox)
    HeartCountEditBox: EditBox = null;

    @property(EditBox)
    SavingPotEditBox: EditBox = null;

    @property(Button)
    winButton: Button = null;

    @property(Button)
    loseButton: Button = null;

    @property(Button)
    editButton: Button = null;

    @property(Button)
    clearButton: Button = null;

    @property(Button)
    profilerButton: Button = null;

    @property(Button)
    applovinDebuggerButton: Button = null;

    @property(Button)
    cloudDeleteButton: Button = null;

    @property(Button)
    cloudSaveButton: Button = null;

    @property(Button)
    cloueLoadButton: Button = null;

    @property(Button)
    requestPermissionButton: Button = null;

    @property(Button)
    scheduleButton: Button = null;

    @property(Button)
    removeScheduleButton: Button = null;

    @property(Button)
    gameHudHideButton: Button = null;

    @property(Button)
    pauseButton: Button = null;

    @property(Button)
    resumeButton: Button = null;

    willBind() {
        this.node.active = DevConfig.DEVELOPER_TOOLS;
    }

    useObserves() {
        return [
            udm.stageNum.subscribe(_ => this.StageNumEditBox.string = `${udm.stageNum.value}`),
            udm.coin.subscribe(_ => this.CoinCountEditBox.string = `${udm.coin.value}`),
            udm.heart.subscribe(_ => this.HeartCountEditBox.string = `${udm.heart.value.heart}`),
            udm.savingPot.subscribe(_ => {
                this.SavingPotEditBox.string = `${udm.savingPot.value.coin ?? 0}`;
            }),

            BindEditBoxInputComplete(this.StageNumEditBox).subscribe(_ => {
                develop_func.debug_set_stage(Number(this.StageNumEditBox.string));
            }),

            BindEditBoxInputComplete(this.CoinCountEditBox).subscribe(_ => {
                develop_func.debug_set_coin(Number(this.CoinCountEditBox.string));
            }),

            BindEditBoxInputComplete(this.HeartCountEditBox).subscribe(_ => {
                develop_func.debug_set_heart(Number(this.HeartCountEditBox.string));
            }),

            BindEditBoxInputComplete(this.SavingPotEditBox).subscribe(_ => {
                develop_func.debug_set_savingpot_coin(Number(this.SavingPotEditBox.string));
            }),

            BindButton(this.winButton).subscribe(this.winButtonClicked.bind(this)),
            BindButton(this.loseButton).subscribe(this.loseButtonClicked.bind(this)),
            BindButton(this.editButton).subscribe(this.editButtonClicked.bind(this)),

            BindButton(this.clearButton).subscribe(this.clearButtonClicked.bind(this)),

            BindButton(this.profilerButton).subscribe(this.profilerButtonClicked.bind(this)),
            BindButton(this.applovinDebuggerButton).subscribe(this.applovinDebuggerButtonClicked.bind(this)),

            BindButton(this.cloudDeleteButton).subscribe(this.cloudDeleteButtonClicked.bind(this)),
            BindButton(this.cloudSaveButton).subscribe(this.cloudSaveButtonClicked.bind(this)),
            BindButton(this.cloueLoadButton).subscribe(this.cloueLoadButtonClicked.bind(this)),

            BindButton(this.requestPermissionButton).subscribe(this.requestPermissionButtonClicked.bind(this)),
            BindButton(this.scheduleButton).subscribe(this.scheduleButtonClicked.bind(this)),
            BindButton(this.removeScheduleButton).subscribe(this.removeScheduleButtonClicked.bind(this)),

            BindButton(this.gameHudHideButton).subscribe(this.gameHudHideButtonClicked.bind(this)),

            BindButton(this.pauseButton).subscribe(this.pauseButtonClicked.bind(this)),
            BindButton(this.resumeButton).subscribe(this.resumeButtonClicked.bind(this)),
        ];
    }

    onLoadView(): void {
        thinking_sdk_m.requestGetDeviceId();
    }

    winButtonClicked() {
        gm.forceWinCommand.execute();
    }

    loseButtonClicked() {
        gm.forceLoseCommand.execute();
    }

    clearButtonClicked() {
        develop_func.debug_clear_all();
    }

    profilerButtonClicked() {
        if (profiler.isShowingStats()) {
            profiler.hideStats();
        } else {
            profiler.showStats();
        }
    }

    applovinDebuggerButtonClicked() {
        applovin_sdk_m.requestDebugger();
    }

    cloudDeleteButtonClicked() {
        cloudstorage_sdk_m.requestDeleteGameData();
    }

    cloudSaveButtonClicked() {
        cloudstorage_sdk_m.requestSaveGameData(udm.stringify());
    }

    cloueLoadButtonClicked() {
        cloudstorage_sdk_m.requestLoadGameData();
    }

    requestPermissionButtonClicked() {
        notification_sdk_m.requestPermission();
    }

    scheduleButtonClicked() {
        let date = timem.getCurrentDate();
        date.setSeconds(date.getSeconds() + 30);
        notification_sdk_m.requestScheduleNotification(
            "free_coin",
            "Color Slide",
            "Free coins are waiting for you!",
            date
        );
    }

    removeScheduleButtonClicked() {
        notification_sdk_m.requestRemoveNotification('free_coin')
    }

    gameHudHideButtonClicked() {
        gm.gameHudHide.value = !gm.gameHudHide.value;
    }

    editButtonClicked() {
        lm.flowStatus.value = FlowStatus.EditorGame; // 返回编辑模式
    }

    pauseButtonClicked() {
        am.pauseReasons.add('test_pause');
    }

    resumeButtonClicked() {
        am.pauseReasons.delete('test_pause');
    }
}

